package itsm.isperp.module.service.project;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.DataRequest;
import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.serviceCatalog.SrcService;
import itsm.isperp.module.repository.project.ProjProjectServiceMapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class ProjProjectServiceService extends
		BaseService<itsm.isperp.module.entity.project.ProjProjectService> {

	@Autowired
	protected ProjProjectServiceMapper projProjectServiceMapper;

	@Override
	public ProjProjectServiceMapper getRepository() {
		return this.projProjectServiceMapper;
	}

	/**
	 * 批量增加项目服务项目
	 * 
	 * @param projectId
	 *            项目id
	 * @param serviceIds
	 *            服务id列表
	 */
	public void saveProjectServices(String projectId,
			Collection<String> serviceIds) {

		String sql = "INSERT INTO proj_project_service(id,proj_project_id,src_service_id)VALUES(?,?,?)";

		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		List<Object[]> list = new ArrayList<>();

		if (serviceIds != null && serviceIds.size() > 0) {
			for (String m : serviceIds) {
				Object[] arg = new Object[3];
				arg[0] = HibernateUUIDGenerator.generate();
				arg[1] = projectId;
				arg[2] = m;

				list.add(arg);
			}

			jdbcTemplate.batchUpdate(sql, list);
		}
	}

	/**
	 * 
	 * 查询项目可选的服务(项目不包含)
	 * 
	 * @param req
	 *            数据请求
	 * @return 数据结果
	 */
	public DataResponse<SrcService> findAllServiceByRequest(String projectId,
			DataRequest req) {
		return this.projProjectServiceMapper.findAllOptionalByRequest(
				projectId, req);
	}

	/**
	 * 查询项目包含的服务
	 * 
	 * @param projectId
	 * @param req
	 * @return
	 */
	public DataResponse<SrcService> getByProjectIds(String projectIds,
			DataRequest req) {

		return this.projProjectServiceMapper.findByProjectId(projectIds, req);
	}

	public DataResponse<SrcService> getByProjectId(List<String> projectIds,
			DataRequest req) {
		String projectId = "'" + projectIds.get(0) + "'";

		for (int i = 1; i < projectIds.size(); i++) {
			projectId += ",'" + projectIds.get(0) + "'";
		}
		return this.projProjectServiceMapper.findByProjectId(projectId, req);
	}

	public DataResponse<SrcService> getByProjectId(String projectId,
			DataRequest req) {

		return this.projProjectServiceMapper.findByProjectId("'" + projectId
				+ "'", req);
	}
	
	public DataResponse<SrcService> getAllSrcService(
			DataRequest req) {

		return this.projProjectServiceMapper.findAllSrcService(req);
	}

	/**
	 * 根据项目Id删除项目管理的服务
	 * 
	 * @param projectId
	 *            项目id
	 */
	public void deleteProjectService(String projectId) {
		this.projProjectServiceMapper.deleteByProjectId(projectId);
	}

	/**
	 * 根据id删除实体
	 * 
	 * @param id
	 *            不能 {@literal null}.
	 */
	public void deleteProjectServiceById(String id) {
		this.projProjectServiceMapper.deleteByClass(id,
				itsm.isperp.module.entity.project.ProjProjectService.class);
	}

	public List<itsm.isperp.module.entity.project.ProjProjectService> findAllByProjectId(
			String id) {

		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		req.addFilterRule("proj_project_id", JqGridFilterRuleOp.eq, id);

		return this.findAll(req);
	}

}
